package br.com.sgmf.dao;

import br.com.sgmf.model.Motorista;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Repository
public class MotoristaDAO {

    @PersistenceContext
    private EntityManager manager;

    @Transactional
    public void save(Motorista motorista) {
        manager.persist(motorista);
    }

    @Transactional
    public void update(Motorista motorista) {
        manager.merge(motorista);
    }

    @Transactional
    public void delete(Motorista motorista) {
        manager.remove(motorista);
    }

    public Motorista findById(Integer id) {
        return manager.find(Motorista.class, id);
    }

    @SuppressWarnings("unchecked")
    public List<Motorista> findAll() {
        return manager.createQuery("SELECT f FROM Motorista f").getResultList();
    }

}
